package org.xxpay.manage.sys.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xxpay.core.common.annotation.MethodLog;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.common.util.BtUtil;
import org.xxpay.core.common.util.JsonUtil;
import org.xxpay.core.entity.SysLoginFailIpRecord;
import org.xxpay.manage.common.ctrl.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: pangxaioyu
 * @date: 20/11/12
 * @description:
 */
@Controller
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/sys/login_fail_ip_record")
public class SysLoginFailIpRecordController extends BaseController {

    /**
     * 查询IP记录
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String id = getStringRequired(param, "id");
        SysLoginFailIpRecord record = commonService.sysLoginFileIpRecordService.findById(id);
        return ResponseEntity.ok(XxPayResponse.buildSuccess(record));
    }

    /**
     * 新增屏蔽IP
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    @MethodLog( remark = "新增屏蔽IP" )
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String remark = getString(param, "remark");
        String ip = getString(param, "ip");
        Byte loginToBlackType = getByteRequired(param, "loginToBlackType");
        // 宝塔参数配置
        JSONObject sysConfig = commonService.sysConfigService.getSysConfig();
        String btUrlConfig = sysConfig.getString("btUrlConfig");
        String btSignConfig = sysConfig.getString("btSignConfig");
        Integer loginCountConfig = sysConfig.getInteger("loginCountConfig");
        if (MchConstant.LOGIN_TO_BLACK_SYS == loginToBlackType) {       // 系统拉黑
            SysLoginFailIpRecord dbRecord = commonService.sysLoginFileIpRecordService.findById(ip);
            if (dbRecord != null) return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_RECORD_EXISTS));
            // 存入DB
            SysLoginFailIpRecord record = new SysLoginFailIpRecord();
            record.setIp(ip);
            record.setFailCount(loginCountConfig);
            record.setLoginToBlackType(MchConstant.LOGIN_TO_BLACK_SYS);
            record.setRemark(remark);
            int add = commonService.sysLoginFileIpRecordService.add(record);
            if (add != 1) return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        }else {         // 宝塔拉黑
            JSONObject json = JsonUtil.getJSONObjectFromJson(BtUtil.addShieldIp(btSignConfig, btUrlConfig, ip, remark));
            if (!json.getBoolean("status")) return ResponseEntity.ok(XxPayResponse.buildSuccess(json));
        }
        return ResponseEntity.ok(XxPayResponse.buildSuccess());
    }

    /**
     * 防火墙IP列表
     * @param request
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Byte loginToBlackType = getByteRequired(param, "loginToBlackType");
        // 宝塔参数配置
        JSONObject sysConfig = commonService.sysConfigService.getSysConfig();
        if (MchConstant.LOGIN_TO_BLACK_BT == loginToBlackType) {
            String btSignConfig = sysConfig.getString("btSignConfig");
            String btUrlConfig = sysConfig.getString("btUrlConfig");
            List<JSONObject> ipList = BtUtil.getIpList(btSignConfig, btUrlConfig);
            return ResponseEntity.ok(XxPayPageRes.buildSuccess(ipList));
        }else {
            Integer loginCountConfig = sysConfig.getInteger("loginCountConfig");
            SysLoginFailIpRecord record = new SysLoginFailIpRecord();
            record.setLoginToBlackType(MchConstant.LOGIN_TO_BLACK_SYS);
            List<SysLoginFailIpRecord> recordList = commonService.sysLoginFileIpRecordService.find(record);
            List<JSONObject> objects = new LinkedList<>();
            for (SysLoginFailIpRecord list:recordList) {
                JSONObject object = (JSONObject) JSON.toJSON(list);
                objects.add(object);
                if (list.getFailCount() >= loginCountConfig) {
                    object.put("status", "1");
                }else {
                    object.put("status", "0");
                }
            }
            return ResponseEntity.ok(XxPayPageRes.buildSuccess(objects));
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @MethodLog( remark = "删除" )
    public ResponseEntity<?> delete(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Byte loginToBlackType = getByte(param, "loginToBlackType");
        if (loginToBlackType == null) {
            String ip = getStringRequired(param, "ip");
            String id = getStringRequired(param, "id");
            // 宝塔参数配置
            JSONObject sysConfig = commonService.sysConfigService.getSysConfig();
            String btSignConfig = sysConfig.getString("btSignConfig");
            String btUrlConfig = sysConfig.getString("btUrlConfig");

            JSONObject json = JsonUtil.getJSONObjectFromJson(BtUtil.removeShieldIp(btSignConfig, btUrlConfig, id, ip));
            if (!json.getBoolean("status")) return ResponseEntity.ok(XxPayResponse.buildSuccess(json));
            commonService.sysLoginFileIpRecordService.delete(ip);
        }else {
            String ip = getStringRequired(param, "ip");
            commonService.sysLoginFileIpRecordService.delete(ip);
        }

        return ResponseEntity.ok(XxPayResponse.buildSuccess());
    }

}
